import axios from 'axios'
//引入element-ui
import ElementUI from 'element-ui';

import router from './router'
import store from './store'
import ro from "element-ui/src/locale/lang/ro";

axios.defaults.baseURL = "http://localhost:8081"
//前置拦截
axios.interceptors.request.use(configurable =>{
  console.log("============")
  return configurable;
})

//后置拦截
axios.interceptors.response.use(
  response =>{
    let res = response.data;

    console.log("============")
    console.log(res)
    console.log("============")

    if(res.code === 200){
      return response
    }else{
      // 弹窗异常信息
      ElementUI.Message({
        message: response.data.msg,
        type: 'error',
        duration: 2 * 1000
      })
      // 直接拒绝往下面返回结果信息
      return Promise.reject(response.data.msg)
    }
  },
  error => {
    console.log(error)
    if(error.response.data){
      error.message = error.response.data.msg;
    }
    // 根据请求状态觉得是否登录或者提示其他
    if (error.response.status === 401) {
      store.commit('REMOVE_INFO');
      router.push({
        path: '/login'
      });
      error.message = '请重新登录';
    }
    if (error.response.status === 403) {
      error.message = '权限不足，无法访问';
    }
    ElementUI.Message({
      message: error.message,
      type: 'error',
      duration: 3 * 1000
    })
    return Promise.reject(error)
  })
